﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CardDataWCFService
{
    public static class DataTransformer
    {
        public static Contracts.Player Transform(Player p)
        {
            Contracts.Player p2 = new CardDataWCFService.Contracts.Player();
            p2.PlayerID = p.PlayerID;
            p2.PlayerName = p.PlayerName;

            return p2;
        }

        public static Player Transform(Contracts.Player p)
        {
            Player p2 = new Player();
            p2.PlayerID = p.PlayerID;
            p2.PlayerName = p.PlayerName;

            return p2;
        }

        public static List<Contracts.Player> Transform(List<Player> ps)
        {
            List<Contracts.Player> ps2 = new List<CardDataWCFService.Contracts.Player>();
            foreach (Player p in ps)
            {
                Contracts.Player p2 = new CardDataWCFService.Contracts.Player();
                p2.PlayerID = p.PlayerID;
                p2.PlayerName = p.PlayerName;
                ps2.Add(p2);
            }

            return ps2;
        }

        public static List<Player> Transform(List<Contracts.Player> ps)
        {
            List<Player> ps2 = new List<Player>();
            foreach (Contracts.Player p in ps)
            {
                Player p2 = new Player();
                p2.PlayerID = p.PlayerID;
                p2.PlayerName = p.PlayerName;
                ps2.Add(p2);
            }

            return ps2;
        }

        public static HeldBy Transform(Contracts.HeldBy hb)
        {
            HeldBy hb2 = new HeldBy();
            hb2.CardID = hb.CardID;
            hb2.PlayerID = hb.PlayerID;
            return hb2;
        }

        public static Contracts.HeldBy Transform(HeldBy hb)
        {
            Contracts.HeldBy hb2 = new Contracts.HeldBy();
            hb2.CardID = hb.CardID;
            hb2.PlayerID = hb.PlayerID;
            return hb2;
        }

        public static Contracts.Card Transform(Card c)
        {
            Contracts.Card c2 = new CardDataWCFService.Contracts.Card();
            c2.CardID = c.CardID;
            c2.CardRFID = c.CardRFID;
            c2.Suit = c.Suit;
            c2.Type = c.Type;

            return c2;
        }

        public static Card Transform(Contracts.Card c)
        {
            Card c2 = new Card();
            c2.CardID = c.CardID;
            c2.CardRFID = c.CardRFID;
            c2.Suit = c.Suit;
            c2.Type = c.Type;

            return c2;
        }

        public static Contracts.PlayerCards Transform(PlayerCard pc)
        {
            Contracts.PlayerCards pc2 = new Contracts.PlayerCards();
            pc2.CardID = pc.CardID;
            pc2.CardRFID = pc.CardRFID;
            pc2.PlayerID = pc.PlayerID;
            pc2.PlayerName = pc.PlayerName;
            pc2.Suit = pc.Suit;
            pc2.Type = pc.Type;
            pc2.ImageFileName = pc.ImageFileName;

            return pc2;
        }

        public static PlayerCard Transform(Contracts.PlayerCards pc)
        {
            PlayerCard pc2 = new PlayerCard();
            pc2.CardID = pc.CardID;
            pc2.CardRFID = pc.CardRFID;
            pc2.PlayerID = pc.PlayerID;
            pc2.PlayerName = pc.PlayerName;
            pc2.Suit = pc.Suit;
            pc2.Type = pc.Type;
            pc2.ImageFileName = pc.ImageFileName;

            return pc2;
        }

        public static Dictionary<Contracts.Player, List<Contracts.PlayerCards>> Transform(Dictionary<Player, List<PlayerCard>> pcs)
        {
            Dictionary<Contracts.Player, List<Contracts.PlayerCards>> pcs2 = new Dictionary<Contracts.Player,List<Contracts.PlayerCards>>();
            foreach(Player player in pcs.Keys)
	        {
                //Get the list of LINQ cards
                List<PlayerCard> outPc = null;
                pcs.TryGetValue(player, out outPc);

                //Translate the list of LINQ cards to DataContract cards
                List<Contracts.PlayerCards> pc2 = new List<Contracts.PlayerCards>();
                foreach (PlayerCard pc in outPc)
                {
                    pc2.Add(DataTransformer.Transform(pc));
                }

                //Add this player and their cards to the list
                pcs2.Add(DataTransformer.Transform(player), pc2);
	        }

            return pcs2;
        }
    }
}
